/**
 * 开关
 */
var CustomToggle = ccui.Layout.extend({
    /**
     * 一般状况下显示的图
     * @type [string]
     */
    normalImages: [],
    /**
     * 焦点状况下显示的图
     * @type [string]
     */
    focusImages: [],
    /**
     * 受依赖的
     * @type [string]
     */
    dependObjects: [],
    /**
     * 显示的组
     */
    showObjects: [],
    /**
     * 隐藏的组
     */
    hideObjects: [],
    /**
     * 组策略
     * @type {String}
     */
    group: null,
    /**
     * @type {cc.Node}
     */
    parentNode: null,

    /**
     * 是否是focus状态
     * @type {boolean}
     */
    IsOn: false,

    /**
     * 特殊事件数组
     * @type[function]
     */
    handles: [],

    /**
     * 是否可点击
     */
    interactable: true,

    ctor: function (parentNode) {
        this._super();
        this.parentNode = parentNode;
        this.handles = [];
        this.setTouchEnabled(true);
        this.addClickEventListener(this.onClick.bind(this));
    },
    onEnterTransitionDidFinish: function () {
        ccui.Layout.prototype.onEnterTransitionDidFinish.call(this);
        this.changeFocus();
    },
    /**
     * 点击回调
     */
    onClick: function () {
        if (!this.interactable) {
            return;
        }
        if (this.group == null) {
            this.IsOn = !this.IsOn;
            this.changeFocus();
        } else {
            var groupObj = this.getNode(this.parentNode, this.group);
            if (this.IsOn && groupObj.allowSwitchOff) {
                this.IsOn = false;
                this.changeFocus();
            } else {
                groupObj.changeOn(this);
            }
        }
        for (var i = 0; i < this.handles.length; i++) {
            var handle = this.handles[i];
            handle();
        }
    },
    /**
     * 改变状态
     */
    changeFocus: function () {
        var self = this;
        _.forIn(self.normalImages, function (normalImage) {
            var normalImageObj = self.getNode(self.parentNode, normalImage);
            safeVisible(normalImageObj, !self.IsOn);
        });
        _.forIn(self.focusImages, function (focusImage) {
            var focusImageObj = self.getNode(self.parentNode, focusImage);
            safeVisible(focusImageObj, self.IsOn);
        });
        _.forIn(self.dependObjects, function (dependObject) {
            var dependObjectObj = self.getNode(self.parentNode, dependObject);
            safeVisible(dependObjectObj, self.IsOn && self.isVisible());
        });
        if (!this.IsOn)
            return;
        _.forIn(self.showObjects, function (showObject) {
            var showObjectObj = self.getNode(self.parentNode, showObject);
            safeVisible(showObjectObj, self.isVisible());
        });
        _.forIn(self.hideObjects, function (hideObject) {
            var hideObjectObj = self.getNode(self.parentNode, hideObject);
            safeVisible(hideObjectObj, false);
        });
    },
    /**
     * 取得node
     * @param node
     * @param str
     */
    getNode: function (node, str) {
        var names = str.split(".");
        for (var i = 0; i < names.length; i++) {
            node = node[names[i]];
        }
        return node;
    },
    /**
     * 添加事件
     * @param {function} func
     * @public
     */
    addHandle: function (func) {
        this.handles.push(func);
    },
    /**
     * 清除事件
     * @public
     */
    clearHandle: function () {
        this.handles = [];
    }
});